<template>
  <div>
    <!-- 添加sku -->
    <el-form label-width="80px">
      <el-form-item label="SPU名称"> {{spu.spuName}} </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" v-model="skuInfo.price" type="number"></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" placeholder="规格描述" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item :label="attr.attrName" v-for="(attr,index) in attrInfoList" :key="attr.id">
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`" v-for="(attrValue,index) in attr.attrValueList" :key="attrValue.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性" >
        <el-form :inline="true" label-width="80px">
          <el-form-item :label="SaleAttr.saleAttrName" v-for="(SaleAttr,index) in spuSaleAttrList" :key="SaleAttr.id">
            <el-select placeholder="请选择" v-model="SaleAttr.attrIdAndValueId">
              <el-option :label="SaleAttrValue.saleAttrValueName" :value="`${SaleAttr.id}:${SaleAttrValue.id}`" v-for="(SaleAttrValue,index) in SaleAttr.spuSaleAttrValueList" :key="SaleAttrValue.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table style="width: 100%" border :data="spuImageList" @selection-change="handleSelectionChange">
          <el-table-column
            width="80"
            type="selection"
          ></el-table-column>
          <el-table-column label="图片">
          <template slot-scope="{row,$index}">
            <img :src="row.imgUrl" alt="" style="width=100px;height:100px">
          </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
          <template slot-scope="{row,$index}">
           <el-button type="primary" size="medium" v-if="row.isDefault==0" @click="changeDefault(row)">设置默认</el-button>
           <el-button type="" size="medium" v-else>默认</el-button>
          </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      spuImageList: [],
      spuSaleAttrList: [], //销售属性
      attrInfoList: [], //平台属性的数据
      //收集图片的数据
      imageList:[],
      //收集sku信息
      skuInfo: {
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类数据是通过表单数据双向绑定收集的
        skuName: "",
        price: "",
        weight: "",
        skuDesc: "",
        //第三类
        skuDefaultImg: "",
        //平台属性
        skuAttrValueList: [
        //   {
        //     attrId: 0,
        //     valueId: 0,
        //   },
        ],
  
        skuImageList: [
        //   {
        //     imgName: "",
        //     imgUrl: "",
        //     isDefault: "",
        //     spuImgId: 0,
        //   },
        ],
        //销售属性
        skuSaleAttrValueList: [
        //   {
        //     saleAttrId: 0,
        //     saleAttrValueId: 0,
        //   },
        ],

        
      },
      spu:{}
    };
  },
  methods: {
    async getData(category1Id, category2Id, row) {
      //收集父组件传来的数据
      this.skuInfo.category3Id = row.category3Id;
      this.skuInfo.spuId = row.id;
      this.skuInfo.tmId = row.tmId;
      this.spu = row;
      let result = await this.$api.spu.reqSpuImageList(row.id);
      if (result.code == 200) {
        let list = result.data;
        list.forEach(item => {
            item.isDefault = 0;
        });
        this.spuImageList = list;
      }
      let result1 = await this.$api.spu.reqSpuSaleAttrList(row.id);
      if (result1.code == 200) {
        this.spuSaleAttrList = result1.data;
      }
      let result2 = await this.$api.spu.reqAttrInfoList(
        category1Id,
        category2Id,
        row.category3Id
      );
      if (result2.code == 200) {
        this.attrInfoList = result2.data;
      }
    },
    //表格复选框按钮的事件
    handleSelectionChange(selection){
        //收集的数据缺少isDefault字段
        this.imageList = selection
    },
    //排他操作
    changeDefault(row){
        this.spuImageList.forEach(item => {
            item.isDefault = 0;
        })
        row.isDefault = 1;
        //收集默认图片地址
        this.skuInfo.skuDefaultImg = row.imgUrl
    },
    cancel(){
        this.$emit('changeScenes',0);
        Object.assign(this._data,this.$options.data())
    },
   async save(){
        //整理参数
        //整理平台属性数据方式1
        // let arr = []
        const {attrInfoList, skuInfo,spuSaleAttrList,imageList} = this;
        // attrInfoList.forEach(item=>{
        //     //代表选择了平台属性
        //     if (item.attrIdAndValueId) {
        //        const [attrId,valueId] = item.attrIdAndValueId.split(":");
        //        let obj = {attrId,valueId};
        //        arr.push(obj)
        //     }
        // })
        // //将整理好的数组赋值给
        // this.skuInfo.skuAttrValueList = arr;

        //整理平台属性数据方式2
       skuInfo.skuAttrValueList = attrInfoList.reduce((prev,item) => {
            if (item.attrIdAndValueId) {
               const [attrId,valueId] = item.attrIdAndValueId.split(":");
               prev.push({attrId,valueId});
            }
            return prev;
        },[]);

        //整理销售属性
       skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev,item) => {
            if (item.attrIdAndValueId) {
                const [saleAttrId,saleAttrValueId] = item.attrIdAndValueId.split(":");
                prev.push({saleAttrId,saleAttrValueId});
            }
            return prev;
        },[]);

        //整理图片数据
        skuInfo.skuImageList = imageList.map(item => {
            return {
                imgName:item.imgName,
                imgUrl:item.imgUrl,
                isDefault:item.isDefault,
                spuImgId:item.id
            }
        });

        //发请求
      let result = await this.$api.spu.reqAddSku(skuInfo);
      if (result.code == 200) {
        this.$message.success("添加成功！")
        this.$emit('changeScenes',0);
      }
    }
  },
};
</script>

<style></style>
